# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgname=pacman
pkgver=5.2.1
pkgrel=6
contrib_ver=1.2.0
pkgdesc="A library-based package manager with dependency support (MSYS2 port)"
arch=('i686' 'x86_64')
url="https://www.archlinux.org/pacman/"
license=('GPL')
groups=('base' 'base-devel')
depends=('bash>=4.2.045'
         #'dash'
         #'busybox'
         'gettext'
         'gnupg'
         'msys2-runtime'
         #'libarchive'
         'curl'
         #'libgpgme'
         'pacman-mirrors'
         'msys2-keyring'
         'which'
         'bzip2'
         'xz'
         'zstd')
optdepends=('diffutils' 'vim')
checkdepends=('python2')
makedepends=('asciidoc'
             'autoconf'
             'automake-wrapper'
             'doxygen'
             'libtool'
             'git'
             'gettext-devel'
             'heimdal-devel'
             'libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel'
             'libsqlite-devel'
             'libunistring-devel')
provides=('pacman-contrib')
conflicts=('pacman-contrib')
replaces=('pacman-contrib')
# options=('!strip' 'debug')
backup=("etc/pacman.conf"
        "etc/makepkg.conf"
        "etc/makepkg_mingw32.conf"
        "etc/makepkg_mingw64.conf")
source=(https://sources.archlinux.org/other/pacman/${pkgname}-${pkgver}.tar.gz{,.sig}
        https://git.archlinux.org/pacman-contrib.git/snapshot/${pkgname}-contrib-${contrib_ver}.tar.gz
        "pacman.conf"
        "makepkg.conf"
        "makepkg_clang32.conf"
        "makepkg_clang64.conf"
        "makepkg_mingw32.conf"
        "makepkg_mingw64.conf"
        "makepkg-clang"
        "makepkg-mingw"
        "0000-pacman-msysize.patch"
        "0001-more-debugging-info.patch"
        "0002-Add-util-msys2.-c-h-and-rebase-db-msys2.-c.patch"
        "0003-use-busybox-for-msys2-post-installs.patch"
        "0004-Link-pacman-with-static-libraries.patch"
        "0005-Hack-gettext-libalpm-pkg-config-static-link.patch"
        "0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch"
        "0007-exe-interp-ignore-file-conflict.patch"
        "0008-answer-yes-by-default.patch"
        "0009-msys-use-pipe-instead-socket.patch"
        "0010-filelist-strcasecmp.patch"
        "0011-update-ja-po-for-msys2.patch"
        "0012-workaround-bsdtar-AD-account.patch"
        "0100-contrib.patch"
        "0101-contrib-pactree-gpgdir.patch"
        Doxyfile.in)
validpgpkeys=('6645B0A8C7005E78DB1D7864F99FFE0FEAE999BD')
sha256sums=('1930c407265fd039cb3a8e6edc82f69e122aa9239d216d9d57b9d1b9315af312'
            'SKIP'
            '317f53819e35647a19138cb0d68e16206af4a80f52115a7cd622c4a367f914b7'
            '7849538ac2b89f4e9881ad5db78a2aa37a2ddb936f87a0ede1c2322e515918fe'
            '40a46147ebb9b85f74d60d53a1bf911dfb32d85c65c2ee6e57bfd6d3d4aac848'
            '4469ac36c628d123c46feacc34db7ab763a7bfcfb860461846200c68a7d62e2e'
            '13b20f6833df76a34ca38376469e275f93552ef9063d9ca06fc3c9ef5e87aa0b'
            '9334a89f67b8f4f3427d8cf60969fb4fa9794b12c21c002f823270c45c63784d'
            '70c01a440acc2ddfff1c3d5d52f48865a8a33c3f8b494fc77974c398aa5be8e0'
            '8949cfd1fee9c965ec8afccee27937d4c15b4cccb2c367db4fb2b718bc66e74a'
            '684648d5b1246af9ce5b3afaadf201873269c5208057e8cbdbd409b4c0f22564'
            '1f92a6dcb3ae631cb0bbf5e089417a2ff7b895e541739f3a49b126b80f185920'
            '24ea2c8dca37847e04894ebfd05d1cf5df49dc0c8089f5581c99caa19b77a7ef'
            '870b197b7d6379a9c1ebb5c449c902b21d75ec21e966a2e54af82501465180f7'
            '23132552a388b238acf8bf650b5c2aa08cf3de63c647e84ad551807c4edfeb1e'
            '65906109a7b71a9b836566ea888532b41f4ba6ea5996f1f3303c5789e03960cb'
            '5d2b274cc8e3f68c7b0b74db466006f52610be9b35179d8cd854a9611c981de6'
            'e2731c4007a7c78848679e2203c35d1e56d3ef15a7586aeac9cc9e6e97824cfb'
            '52343bbf99da6408950e34dd84319e34724040f866c497c25e36b4ee54030e82'
            'e4f6e17af19e17e745a9f1c6b8402f5896229062c82167cb61f8e7d29eda716c'
            '9e8fe5ee78192b0407e80ad2e52cb27569c35974b6c26e465e3d55e19c03d108'
            '93be4523fb8c3dd6b56eddfe0b09e666725a62eb43392fee336ba1a328f9ffdd'
            '205b1655ea4cfac248aa8d268017979db0644ff2f627e5969c30547fdc9bcd8e'
            'c242202f3526d5eaa21d321a1d0fa472697c50e8288175ebe93212356c78d1f1'
            '78a314a11377f7c9eb7bcd5f103a32734f2df0b3696df50bd38c7f6c754154d0'
            '4ff3f10a99e894f3ae8576227f60bff7e21a1d7a775f89d936e43501693e9618'
            '8cc0a7b1a3390a8990cef242ac4a84d4eb03f367bc2a01c883908ff7c8d59427')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/0000-pacman-msysize.patch
  patch -p1 -i ${srcdir}/0001-more-debugging-info.patch
  # patch -p1 -i "${srcdir}"/0002-Add-util-msys2.-c-h-and-rebase-db-msys2.-c.patch
  # patch -p1 -i "${srcdir}"/0003-use-busybox-for-msys2-post-installs.patch
  patch -p1 -i ${srcdir}/0004-Link-pacman-with-static-libraries.patch
  patch -p1 -i ${srcdir}/0005-Hack-gettext-libalpm-pkg-config-static-link.patch
  patch -p1 -i ${srcdir}/0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch
  patch -p1 -i ${srcdir}/0007-exe-interp-ignore-file-conflict.patch
  patch -p1 -i ${srcdir}/0008-answer-yes-by-default.patch
  patch -p1 -i ${srcdir}/0009-msys-use-pipe-instead-socket.patch
  patch -p1 -i ${srcdir}/0010-filelist-strcasecmp.patch
  patch -p1 -i ${srcdir}/0011-update-ja-po-for-msys2.patch
  patch -p1 -i ${srcdir}/0012-workaround-bsdtar-AD-account.patch
  
  cp ${srcdir}/Doxyfile.in doc/

  autoreconf -fi

  cd ${srcdir}/${pkgname}-contrib-${contrib_ver}
  patch -p1 -i ${srcdir}/0100-contrib.patch
  patch -p1 -i ${srcdir}/0101-contrib-pactree-gpgdir.patch
  ./autogen.sh
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  PKG_CONFIG="/usr/bin/pkg-config --static" \
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-static \
    --disable-shared \
    --enable-doc \
    --enable-doxygen \
    --disable-git-version \
    --with-scriptlet-shell=/usr/bin/bash \
    --with-pkg-ext=.pkg.tar.xz

  make -C src/pacman/po update-gmo
  # doxygen segfaults in the first run :( so build again
  make -j3 || make

  cd ${srcdir}/${pkgname}-contrib-${contrib_ver}
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --disable-git-version

  make
  
}

check() {
  make -C "${pkgname}-${pkgver}" check
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make -j1 DESTDIR=${pkgdir} install

  cd ${srcdir}/${pkgname}-contrib-${contrib_ver}
  make -j1 DESTDIR=${pkgdir} install

  # install Arch specific stuff
  install -dm755 ${pkgdir}/etc
  install -m644 ${srcdir}/pacman.conf ${pkgdir}/etc/pacman.conf

  case "${CARCH}" in
  i686)
    mycarch="i686"
    mychost="i686-pc-msys"
    myflags="-march=i686"
  ;;
  x86_64)
    mycarch="x86_64"
    mychost="x86_64-pc-msys"
    myflags="-march=x86-64"
  ;;
  esac

  install -m644 ${srcdir}/makepkg.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_clang32.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_clang64.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw32.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw64.conf ${pkgdir}/etc/
  install -m755 ${srcdir}/makepkg-clang ${pkgdir}/usr/bin/
  install -m755 ${srcdir}/makepkg-mingw ${pkgdir}/usr/bin/

  # set things correctly in the default conf file
  sed -i ${pkgdir}/etc/makepkg.conf \
    -e "s|@CARCH[@]|${mycarch}|g" \
    -e "s|@CHOST[@]|${mychost}|g" \
    -e "s|@CARCHFLAGS[@]|${myflags}|g"
}
